#include<ESP8266WiFi.h>
#include<WiFiClientSecure.h>
#include<ESP8266HTTPClient.h>
#include<string.h>

/**
 * 要连接的WIFI默认账号和密码
**/
char* ssid = "Riven";
char* password = "20160710";


/**
 * 服务器的参数定义和初始化
**/
const char* host = "134.175.41.52";
const int httpsPort = 8080;
String url="http://134.175.41.52:8080/IOTdemo/ESP8266";


/**
 *采集参数的定义和初始化
 **/
int TEM=0;
int HUM=0;
int CO=0;

void httpget();

void httpget()
{
   if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
    HTTPClient http;  //Declare an object of class HTTPClient
    http.begin(url);  //Specify request destination
    int httpCode = http.GET();
    if (httpCode > 0) { //Check the returning code
      String payload = http.getString();   //Get the request response payload
      //Serial.println(payload);                     //Print the response payload
    }//Send the request
    http.end();   //Close connection
  }
}


/**
 *Http请求URL拼接 
 **/
boolean loadData(String temp,String humidity,String co)
{
    int warning = 0;
    int costr =co.toInt();
    if(costr >= 400)
    {
      warning = 1;  
    }
    else
    {
      warning = 0;  
    } 
   url = (String)"http://134.175.41.52:8080/IOTdemo/ESP8266?requestType=updata&temp="+temp+"&humidity="+humidity+"&co="+co+"&warning="+warning;  
   return true;
}


void setup()
{
  Serial.begin(115200);

  WiFi.begin(ssid,password);
  while(WiFi.status() != WL_CONNECTED){
    delay(500);
    //Serial.println('.');
  }
  httpget();
 
}



void loop() {
  Serial.write('W');     //发送WiFi状态信号
  if(Serial.available()>0)
  {
    char flag = Serial.read();
    if(flag == 'D')
     {
         String STR[3] = {};
         int i=0;
         while(Serial.available()>0)
         {
          STR[i] += Serial.read();
          if(Serial.peek() == ',')
          {
            Serial.read();
            i++;
            }
          }
          STR[2] = (String)(STR[2].toInt()*4);
          loadData(STR[0], STR[1], STR[2]);
          httpget();
      } 
    }
    delay(1000);    //Send a request every 30 seconds
  }



  



